

const crud = {

    // 清除数据
    clear(model) {

        this.confirmCall(model, (m) => {
            axios.get(`${m.prefix}/clear`, {params: {}}).then( res => {
                m.$message({
                    type: 'success',
                    message: '操作成功!'
                })
                m.handleUserList()
            }).catch(err => {
                m.$message.error('系统异常！')
            })
        })

    },

    // 复制数据
    copy(model){
        const len = model.multipleSelection.length
        if (len === 0) {
            model.$message.error('请选择至少一条再复制！')
        } else {
            const ids = model.multipleSelection.map(_ => _.id).join(",");
            const tempParams = {
                ids
            }
            axios.get(`${model.prefix}/copy`, {params: tempParams}).then( res => {
                model.$message({
                    type: 'success',
                    message: '复制成功!'
                })
                model.handleUserList()
            }).catch(err => {
                model.$message.error('系统异常！')
            })
        }
    },

    // 清空查询表单
    resetQuery(model) {
        model.resetFormData()
        model.currentPage = 1
        this.handleUserList(model)
    },

    // 关闭弹出框
    closeDialog(model) {
        model.dialogTitle = '新增'
        model.dialogVisible = false
    },

    // 新增弹出框
    add(model) {
        model.dialogTitle = '新增'
        model.resetFormData()
        model.dialogVisible = true
        model.genDestoryBtnShow = false
        model.$refs['formData']?.resetFields()
    },

    // 修改弹出框
    update(model) {
        const selection = model.multipleSelection
        const len = selection.length
        if (len === 1) {
            model.dialogTitle = '修改'

            model.dialogVisible = true
            axios.get(`${model.prefix}/get/${selection[0].id}`, {params: {}}).then( res => {
                model.formData = res.data

                console.log(model.prefix)
                console.log(model.prefix.substr(0, 5))
                if ("/gen/" === model.prefix.substr(0, 5)) {
                    model.genDestoryBtnShow = true
                    if ("none" === model.formData.fieldsExplain) {
                        axios.get(`${model.prefix}/impl/${selection[0].id}`, {params: {}}).then( res => {
                            console.log(res)
                        }).catch(err => {
                            model.$message.error('系统异常！')
                        })
                    }
                }

            }).catch(err => {
                model.$message.error('系统异常！')
            })
        } else {
            const msg = len === 0 ? '请选择一条再修改' : '每次只能修改一条数据'
            model.$message.error(msg)
        }
    },

    // 分页查询数据
    handleUserList(model) {
        let tempParams = model.formQuery
        tempParams.pageNumber = model.currentPage
        tempParams.pageSize = model.pagesize
        axios.get(`${model.prefix}/find`, {params: tempParams}).then( res => {
            model.dataList = res.data.list
            model.totalCount = res.data.totalCount
        }).catch(err => {
            model.$message.error('系统异常！')
        })
    },

    // 保存， 新增或修改
    saveForm(model) {
        model.$refs['formData'].validate((valid) => {
            if (valid) {
                axios.post(`${model.prefix}/save`, model.formData).then( res => {
                    model.dialogVisible = false
                    model.handleUserList()
                }).catch(err => {
                    model.$message.error('系统异常！')
                })
            } else {
                console.log('error submit!!');
                return false;
            }
        })
    },

    // 删除请求
    deleteSome(model) {
        const ids = model.multipleSelection.map(_ => _.id).join(",");
        const tempParams = {
            ids
        }
        axios.get(`${model.prefix}/delete`, {params: tempParams}).then( res => {
            model.$message({
                type: 'success',
                message: '删除成功!'
            })
            model.handleUserList()
        }).catch(err => {
            model.$message.error('系统异常！')
        })
    },

    // 删除确认
    deleteConfirm(model) {
         const len = model.multipleSelection.length
         if (len > 0) {
             this.confirmCall(model, (m) => {
                 this.deleteSome(m)
             })
         } else {
             const msg = '请选择至少一条数据后再修改'
             model.$message.error(msg)
         }
     },

    // 删除确认
    confirmCall(model, cb) {
        model.$confirm('请确认是否要删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            cb(model)
        }).catch(() => {
            model.$message({
                type: 'info',
                message: '已取消删除'
            })
        })
    },

    // 条件查询
    queryBy(model) {
        model.currentPage = 1
        model.handleUserList()
    },

    handleSizeChange(size, model) {
        model.pagesize = size
        model.handleUserList()
    },

    handleCurrentChange(currentPage, model){
        model.currentPage = currentPage
        model.handleUserList()
    },

    genCode(model) {
        axios.get(`${model.prefix}/impl`, {
            params: {
                id: model.formData.id
            }
        }).then( res => {
            model.$message.success('操作成功！')
            model.handleUserList()
        })
            .catch(err => model.$message.error('系统异常！'))
    },


    destroyCode(model) {
        axios.get(`${model.prefix}/destroy/` + model.formData.id , {
            params: {

            }
        }).then( res => {
            model.$message.success('操作成功！')
            model.handleUserList()
        })
            .catch(err => model.$message.error('系统异常！'))
    },

    // open url
    openUrl(model) {
        const len = model.multipleSelection.length
        if (len != 1) {
            model.$message.error('每次只能访问一个网址！')
        } else {
            window.open(model.multipleSelection[0].url)
        }
    },

    // copy cmd
    copyCmd(model) {
        const len = model.multipleSelection.length
        if (len != 1) {
            model.$message.error('每次只能复制一个命令！')
        } else {
            var text = model.multipleSelection[0].cmd
            var content = document.getElementById('copy-content')
            content.value = text
            var content2 = document.getElementById('copy-content')
            content2.select(text)
            document.execCommand("copy");
        }
    },


    // 部署请求
    deploySome(model) {
        const id = model.multipleSelection[0].id
        const tempParams = {
            id
        }

        const loading = model.$loading({
            lock: true,//lock的修改符--默认是false
            text: '正在部署中，部署时间较长，请耐心等待......',//显示在加载图标下方的加载文案
            spinner: 'el-icon-loading',//自定义加载图标类名
            background: 'rgba(0, 0, 0, 0.7)',//遮罩层颜色
            target: document.querySelector('body')//loadin覆盖的dom元素节点
        })

        axios.get(`${model.prefix}/deploy`, {params: tempParams}).then( res => {

            model.$message({
                type: 'success',
                message: '部署成功!'
            })

            model.tableData3 = res.data.mapList
            model.dialogDeployVisible = true

            loading.close()

        }).catch(err => {
            model.$message.error('系统异常！')
            loading.close()
        })
    },

    // 部署确认
    deployConfirm(model) {
        const len = model.multipleSelection.length
        if (len == 1) {
            this.deployCall(model, (m) => {
                this.deploySome(m)
            })
        } else {
            const msg = '每次只能部署一条记录'
            model.$message.error(msg)
        }
    },

    // 部署确认
    deployCall(model, cb) {
        model.$confirm('请确认是否要部署?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            cb(model)
        }).catch(() => {
            model.$message({
                type: 'info',
                message: '已取消部署'
            })
        })
    },


    checkPoint(model) {

        const len = model.multipleSelection.length
        if (len != 1) {
            model.$message.error('每次只能查看一个端口！')
            return
        }

        const loading = model.$loading({
            lock: true,//lock的修改符--默认是false
            text: '正在查看端口，请耐心等待......',//显示在加载图标下方的加载文案
            spinner: 'el-icon-loading',//自定义加载图标类名
            background: 'rgba(0, 0, 0, 0.7)',//遮罩层颜色
            target: document.querySelector('body')//loadin覆盖的dom元素节点
        })

        const id = model.multipleSelection[0].id
        const tempParams = {
            id
        }

        axios.get(`${model.prefix}/check`, {params: tempParams}).then( res => {

            model.tableData3 = res.data.mapList
            model.dialogDeployVisible = true
            loading.close()

        }).catch(err => {
            model.$message.error('系统异常！')
            loading.close()
        })

    },

}